package com.zyx.camunda.helpers.context;

import com.zyx.camunda.common.constants.GenericConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zyx
 * @since 2023/9/21 09:49
 */
public class UserInfoContext {

    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (null == map) {
            map = new HashMap<>();
            THREAD_LOCAL.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (null == map) {
            map = new HashMap<>();
            THREAD_LOCAL.set(map);
        }
        return map.get(key);
    }

    public static void setToken(String token) {
        set(GenericConstant.CONTEXT_KEY_USER_TOKEN, token);
    }

    public static String getToken() {
        Object value = get(GenericConstant.CONTEXT_KEY_USER_TOKEN);
        return returnObjectValue(value);
    }

    public static void setIgnoreUserToken(Boolean ignore) {
        set(GenericConstant.IGNORE_USER_TOKEN, ignore);
    }

    public static String getIgnoreUserToken() {
        Object value = get(GenericConstant.IGNORE_USER_TOKEN);
        return returnObjectValue(value);
    }

    public static void setUserID(String userID) {
        set(GenericConstant.CONTEXT_KEY_USER_ID, userID);
    }

    public static String getUserID() {
        Object value = get(GenericConstant.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value);
    }

    public static void setName(String name) {
        set(GenericConstant.CONTEXT_KEY_USER_NAME, name);
    }

    public static String getName() {
        Object value = get(GenericConstant.CONTEXT_KEY_USER_NAME);
        return returnObjectValue(value);
    }

    public static void setUsername(String username) {
        set(GenericConstant.CONTEXT_KEY_USERNAME, username);
    }

    public static String getUsername() {
        Object value = get(GenericConstant.CONTEXT_KEY_USERNAME);
        return returnObjectValue(value);
    }

    public static void setAccountType(Integer accountType) {
        set(GenericConstant.CONTEXT_KEY_ACCOUNT_TYPE, accountType);
    }

    public static String getAccountType() {
        Object value = get(GenericConstant.CONTEXT_KEY_ACCOUNT_TYPE);
        return returnObjectValue(value);
    }

    public static void setEnterpriseId(Long enterpriseId) {
        set(GenericConstant.CONTEXT_KEY_ENTERPRISE_ID, enterpriseId);
    }

    public static Long getEnterpriseId() {
        Object value = get(GenericConstant.CONTEXT_KEY_ENTERPRISE_ID);
        return returnLongValue(value);
    }

    public static void setEnterpriseStaffId(Long enterpriseStaffId) {
        set(GenericConstant.CONTEXT_KEY_ENTERPRISE_STAFF_ID, enterpriseStaffId);
    }

    public static Long getEnterpriseStaffId() {
        Object value = get(GenericConstant.CONTEXT_KEY_ENTERPRISE_STAFF_ID);
        return returnLongValue(value);
    }

    public static String getEnterpriseStaffName() {
        Object value = get(GenericConstant.CONTEXT_KEY_ENTERPRISE_STAFF_NAME);
        return returnObjectValue(value);
    }

    public static void setEnterpriseStaffName(String enterpriseStaffName) {
        set(GenericConstant.CONTEXT_KEY_ENTERPRISE_STAFF_NAME, enterpriseStaffName);
    }

    public static Boolean getInnerFlag() {
        Object value = get(GenericConstant.CONTEXT_KEY_USER_INNER_FLAG);
        return (Boolean) value;
    }

    public static void setInnerFlag(Boolean innerFlag) {
        set(GenericConstant.CONTEXT_KEY_USER_INNER_FLAG, innerFlag);
    }

    public static void setEnterpriseName(String enterpriseName) {
        set(GenericConstant.CONTEXT_KEY_ENTERPRISE_NAME, enterpriseName);
    }


    public static String getEnterpriseName() {
        Object value = get(GenericConstant.CONTEXT_KEY_ENTERPRISE_NAME);
        return returnObjectValue(value);
    }

    public static void setEnterpriseType(Integer enterpriseType) {
        set(GenericConstant.CONTEXT_KEY_ENTERPRISE_TYPE, enterpriseType);
    }

    public static Integer getEnterpriseType() {
        Object value = get(GenericConstant.CONTEXT_KEY_ENTERPRISE_TYPE);
        return returnIntegerValue(value);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    private static Long returnLongValue(Object value) {
        return value == null ? null : Long.parseLong(value.toString());
    }

    private static Integer returnIntegerValue(Object value) {
        return value == null ? null : Integer.parseInt(value.toString());
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }

}
